import { getLoginApi } from "../../utils/api";
import { getCodeApi, getCodeStatusApi } from "../../utils/wbapi";

const loginModule = {
  state: {
    result: "111",
    code: {},
    status: {},
  },
  mutations: {
    getLoginList(state, payload) {
      // state.result="";
      state.result = payload.data;
      // console.log(payload);
      // console.log(state.result);
    },
    getCode(state, payload) {
      state.code = payload.data;
    },
    getCodeStatus(state, payload) {
      state.status = payload.data;
    },
  },
  actions: {
    async getLoginList({ commit }, payload) {
      return new Promise((resolve) => {
        getLoginApi({
          username: payload.username,
          password: payload.password,
        }).then((res) => {
          resolve();
          commit("getLoginList", { data: res.msg });
          sessionStorage.setItem("status", res.status);
          sessionStorage.setItem("token", res.token);
        });
      });
    },
    async getCode({ commit }, payload) {
      const res = await getCodeApi({
        templateId: "537707",
        phone: payload.phone,
      });
      commit("getCode", { data: res });
    },
    async getCodeStatus({ commit }, payload) {
      return new Promise((resolve) => {
        getCodeStatusApi({
          phone: payload.phone,
          code: payload.code,
        }).then((res) => {
          resolve(res);
          sessionStorage.setItem("phone", res.result.phone);
          commit("getCodeStatus", { data: res.result });
        });
      });
    },
  },
};

export default loginModule;
